﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;

namespace SlamCms.SharePoint.SlamProfiles.ContentItem
{
	internal static class HashtableExtension
	{
		private static Regex divRegex = new Regex("^<div class=\"ExternalClass.*?\">(.*?)<\\/div>$", RegexOptions.Singleline | RegexOptions.Compiled);
		public static void StripContainerDivFromValues(this Hashtable hashtable)
		{
			Dictionary<string, string> itemsToStrip = new Dictionary<string, string>();
			foreach (object keyValue in hashtable.Keys)
			{
				string key = Convert.ToString(keyValue);
				object value = hashtable[key];
				if(value is string && value.ToString().Length < 4000)
				{
					foreach (Match match in divRegex.Matches(value.ToString()))
					{
						itemsToStrip.Add(key.ToString(), match.Groups[1].Value);
						
						break;
					}
				}
			}

			foreach (KeyValuePair<string, string> itemToStrip in itemsToStrip)
			{
				hashtable[itemToStrip.Key] = itemToStrip.Value;
			}
		}
	}
}
